package com.jie.myvhr.controller.salary;

import com.jie.myvhr.bean.RespBean;
import com.jie.myvhr.bean.Salary;
import com.jie.myvhr.service.SalaryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工资账套配置
 *
 * @author jie
 * @date 2019/7/1 -19:18
 */
@RestController
@RequestMapping("/salary/sob")
public class SalaryController {

    @Resource
    private SalaryService salaryService;

    /**
     * 查询账套
     */
    @RequestMapping(value = "/salary", method = RequestMethod.GET)
    public List<Salary> salaries() {
        return salaryService.getAllSalary();
    }

    /**
     * 更新账套
     */
    @PutMapping("/salary")
    public RespBean updateSalary(Salary salary) {
        if (salaryService.updateSalary(salary) == 1) {
            return RespBean.ok("更新成功");
        } else {
            return RespBean.error("更新失败");
        }
    }

    /**
     * 新增账套
     */
    @PostMapping("/salary")
    public RespBean addSalary(Salary salary) {
        if (salaryService.addSalary(salary) == 1) {
            return RespBean.ok("新增成功");
        } else {
            return RespBean.error("新增失败");
        }
    }

    /**
     * 删除账套
     */
    @DeleteMapping("/salary/{ids}")
    public RespBean delSalary(@PathVariable String ids) {
        //删除的时候判断下面是否有员工依赖于账套表
        int result = salaryService.delSalary(ids);
        if (result == 1) {
            return RespBean.ok("删除成功");
        } else if (result > 1) {
            return RespBean.ok("批量删除成功");
        } else if (result == -1) {
            return RespBean.error("该账套下有员工信息，不能删除");
        } else {
            return RespBean.error("删除失败");
        }
    }

}
